﻿using NetFrame;
using NetFrame.auto;
using System;
using System.Collections.Generic;
using System.Collections.Concurrent;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Server.tool;
using MoneyProtocol;
using Server.logic;
using Server.logic.login;


namespace Server
{
    public class HandlerCenter : AbsHandlerCenter
    {
        HandlerInterface login;
        //HandlerInterface user;
        HandlerInterface inout;
        HandlerInterface securityAccount;
        HandlerInterface stock;

        //ConcurrentDictionary<UserToken, long> heartBeat = new ConcurrentDictionary<UserToken, long>();

        //List<UserToken> heartRemoveList = new List<UserToken>();


        // private int time;
        public HandlerCenter()
        {
            login = new LoginHandler();
            inout = new InOutHandler();
            securityAccount = new SecurityAccountHandler();
            stock = new StockHandler();
            // checkHeartBeat();
            //  TimeKeeping();
        }
        //public void TimeKeeping()
        //{
        //    Console.WriteLine(time);
        //    ScheduleUtil.Instance.schedule(delegate
        //    {

        //         time++;
        //        TimeKeeping();
        //    }, 1 * 10);
        //}
        //public void checkHeartBeat()
        //{
        //    Console.WriteLine("checked");
        //    ScheduleUtil.Instance.schedule(delegate
        //    {

        //        long value;
        //        foreach (UserToken item in heartRemoveList)
        //        {
        //            heartBeat.TryRemove(item,out value);
        //        }
        //        heartRemoveList.Clear();
        //        foreach (UserToken item in heartBeat.Keys)
        //        {
        //            if (DateTime.Now.Ticks - heartBeat[item] > 40L)// * 1000 * 1000 * 1000)
        //            {
        //                item.Close(); 
        //                ClientClose(item, "心跳超时");
        //                heartRemoveList.Add(item);
        //            }
        //        }

        //        checkHeartBeat();
        //    }, 1);     
        //}

        public override void ClientClose(UserToken token, string error)
        {
            Console.WriteLine("有客户端断开连接了");


            // user.ClientClose(token, error);
            login.ClientClose(token, error);
        }

        public override void ClientConnect(UserToken token)
        {
            Console.WriteLine("有客户端连接了");
        }

        public override void MessageReceive(UserToken token, object message)
        {
            SocketModel model = message as SocketModel;
            switch (model.type)
            {
                case Protocol.TYPE_LOGIN:
                    login.MessageReceive(token, model);
                    break;
                case Protocol.TYPE_INOUT:
                    inout.MessageReceive(token, model);
                    break;
                case Protocol.TYPE_SECURITYACCOUNT:
                    securityAccount.MessageReceive(token, model);
                    break;
                case Protocol.TYPE_SECURITYRECORD:
                    stock.MessageReceive(token, model);
                    break;
                default:
                    //未知模块  可能是客户端作弊了 无视
                    break;
            }
        }
    }
}
